package com.starhub.engine.document.utils;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.poi.hssf.extractor.ExcelExtractor;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.extractor.XSSFExcelExtractor;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.UnderlinePatterns;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.converter.WordToHtmlConverter;
import org.apache.poi.ss.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class FileExtractor {
    
    // 存储UUID和对应内容的映射
    private static Map<String, Map<String, Object>> contentMap = new HashMap<>();
    
    public static String extractText(InputStream inputStream, FileType fileType, Charset charset) {
        try {
            String text = null;
            if (fileType == FileType.DOC) {
                WordExtractor extractor = new WordExtractor(inputStream);
                text = extractor.getText();
            } else if (fileType == FileType.DOCX) {
                XWPFDocument docx = new XWPFDocument(inputStream);
                XWPFWordExtractor extractor = new XWPFWordExtractor(docx);
                text = extractor.getText();
            } else if (fileType == FileType.XLS) {
                HSSFWorkbook xls = new HSSFWorkbook(inputStream);
                ExcelExtractor extractor = new ExcelExtractor(xls);
                text = extractor.getText();
            } else if (fileType == FileType.XLSX) {
                XSSFWorkbook xlsx = new XSSFWorkbook(inputStream);
                XSSFExcelExtractor extractor = new XSSFExcelExtractor(xlsx);
                text = extractor.getText();
            } else if (fileType == FileType.PDF){
                PDDocument pdfDocument = PDDocument.load(inputStream);
                PDFTextStripper stripper = new PDFTextStripper();
                text = stripper.getText(pdfDocument);
                pdfDocument.close();
            } else if (fileType == FileType.TXT){
                ByteArrayOutputStream buffer = new ByteArrayOutputStream();
                int nRead;
                byte[] data = new byte[1024];
                while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
                    buffer.write(data, 0, nRead);
                }
                buffer.flush();
                text = new String(buffer.toByteArray(), charset);
            }else if (fileType == FileType.MD) {
                // 读取内容作为文本
                ByteArrayOutputStream buffer = new ByteArrayOutputStream();
                int nRead;
                byte[] data = new byte[1024];
                while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
                    buffer.write(data, 0, nRead);
                }
                buffer.flush();
                String markdownContent = new String(buffer.toByteArray(), charset);

                text = markdownContent;
            }
            return text;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
} 